/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Comun;

import java.sql.*;
import javax.swing.JOptionPane;
/**
 *
 * @author
 */
public class Conexion {
    
    private Connection conn;
    private Statement consulta;
    private ResultSet datos;
    private CallableStatement declaracion;
    
    private String servidor;
    private String BD;
    private String usuario;
    private String password;
    private String url;
    private String puerto;
    private static Conexion obj_instancia = null;

    public Conexion() throws InstantiationException, IllegalAccessException {

        try {
            String controlador="com.mysql.jdbc.Driver";
            Class.forName(controlador).newInstance();
            
            servidor = "localhost";
            BD = "bd_calidad";
            puerto = "3306";
            url = "jdbc:mysql://" + servidor + ":"+ puerto + "/" + BD;
            usuario = "richard";
            password = "sakura";

            this.conn = DriverManager.getConnection(url,usuario,password);
            this.consulta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            
            this.conn.setAutoCommit(false);
            
            if (this.conn != null) {
                this.conn.setAutoCommit(false);
            }
        } catch (SQLException exception) {
            System.out.println("Error: " + exception.getMessage());
            JOptionPane.showMessageDialog(null,"Error de conexion... El programa se cerrara \n Intente mas tarde", "Mensaje",1);

        } catch (ClassNotFoundException exception) {
            System.out.println("Error: " + exception.getMessage());
            JOptionPane.showMessageDialog(null,"Error de conexion... El programa se cerrara \n Intente mas tarde", "Mensaje",1);

        }

    }

   
    public static Conexion ObtenerInstancia() throws InstantiationException, IllegalAccessException {
        if (obj_instancia == null) {
            obj_instancia =  new Conexion();
        }
        return obj_instancia;
    }

    public void IniciarConexion() {
        //Inicia una conexión a la BD
        try {
            if (this.conn.isClosed()) {
                this.conn = (DriverManager.getConnection(url, usuario, password));
            }
        } catch (SQLException exception) {
             System.out.println("Error: " + exception.getMessage());
             JOptionPane.showMessageDialog(null,"Error de conexion", "Mensaje",1);
        }
    }

    public void CerrarConexion() {
 
        try {
            this.conn.setAutoCommit(false);
            this.conn.commit();
            this.conn.close();
            
        } catch (SQLException exception) {
            System.out.println("Error: " + exception.getMessage());
            JOptionPane.showMessageDialog(null,"Error de conexion", "Mensaje",1);
        }
    }



    public ResultSet realizarConsulta(String str_sentencia) {

        try {
            this.IniciarConexion();
            this.datos = this.consulta.executeQuery(str_sentencia);
        } catch (SQLException exception) {
            System.out.println("Error: " + exception.getMessage());
            JOptionPane.showMessageDialog(null,"ERROR EN QUERY : " + str_sentencia , "Mensaje",1);
        }

        return this.datos;
    }
    
        public CallableStatement preparaLlamada(String str_sentencia) {

        try {
            this.IniciarConexion();
            this.declaracion = conn.prepareCall(str_sentencia);
        } catch (SQLException exception) {
            System.out.println("Error: " + exception.getMessage());
            JOptionPane.showMessageDialog(null,"ERROR EN QUERY : " + str_sentencia , "Mensaje",1);
        }

        return this.declaracion;
        }
        


}
